package main.helper
{
	import bean.ItemPropertyBean;
	
	import define.CharacterConst;
	import define.DBConst;
	
	import flash.data.SQLConnection;
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.events.Event;
	import flash.events.SQLEvent;
	
	import main.view.Main;
	
	import mx.collections.ArrayCollection;
	import mx.events.FlexEvent;
	
	import orm.armory.vo.TblItemVO;
	
	import sub.event.CharacterEvent;
	
	public class MainHelper
	{
		public function MainHelper()
		{
			if(_instance != null)
				throw new Error("Singleton!");
			_instance = this;
		}
		// 实例
		private static var _instance:MainHelper;
		
		public static function getInstance():MainHelper
		{
			if(_instance == null)
				new MainHelper();
			return _instance;
		}
		// ↑↑↑ Singleton ↑↑↑
		private var _view:Main;
		
		public function creationCompleteListener(event:FlexEvent):void
		{
			this._view = event.currentTarget as Main;
			
			this._view.addEventListener(CharacterEvent.CHARACTER_EVENT_CLICK, slotClickListener);
		}
		public function initializeListener(event:FlexEvent):void
		{
			var armoryConn:SQLConnection = DBConst.armoryConn;
			var irisConn:SQLConnection = DBConst.irisConn;
		}
		private var _searchSlots:Array;
		private var _searchPropClass:String;
		private var _searchPropTalent:String;
		
		private var _optionalItemList:ArrayCollection;
		
		private function slotClickListener(event:CharacterEvent):void
		{
			_searchSlots = event.slot.getSlots();
			_searchPropClass = event.tblCharacterVo.prop_class;
			_searchPropTalent = event.tblCharacterVo.prop_talent;
			
			_optionalItemList = new ArrayCollection();
			
			searchTblItem();
		}
		/**
		 * Step 1
		 * TblItem
		 */
		private function searchTblItem():void
		{
			var stmt:SQLStatement = new SQLStatement();
			stmt.sqlConnection = DBConst.armoryConn;
			// 槽、职业、职能
			stmt.text = "select * from tblitem " + getSqlWhere();
			stmt.itemClass = TblItemVO;
			stmt.addEventListener(SQLEvent.RESULT, getTblItemListListner);
			stmt.execute();
		}
		private var _dataCounts:int;
		private function getTblItemListListner(event:SQLEvent):void
		{
			var r:SQLResult = (event.target as SQLStatement).getResult();
			_dataCounts = r.data.length;
			
			for each(var vo:TblItemVO in r.data)
			{
				var iBean:ItemPropertyBean = new ItemPropertyBean();
				iBean.addEventListener(ItemPropertyBean.EVENT_LOADING_COMPLETED, itemPostCompleted);
				iBean.itemVo = vo;
			}
		}
		private function itemPostCompleted(event:Event):void
		{
			_optionalItemList.addItem(event.target as ItemPropertyBean);
			
			if(_optionalItemList.length == _dataCounts)
				ViewOptionalHelper.getInstance().optionalItemList = _optionalItemList;
		}
		/**
		 * Step 3
		 * infoAcquisition Complete
		 */
//		private function getItemInfomations():void
//		{
//			
//		}
		/**
		 * Step 4
		 * Filter Without Detail
		 */
		
		/**
		 * Step 5
		 * Filter With Detail
		 */
		
		private function getSqlWhere():String
		{
			var s:String = "where ";
			// 添加条件：is_token = false;
			s = s + "is_token = false "
			// 添加条件：槽
			s = s + "and (" + "false "
			for(var i:int = 0; i < _searchSlots.length; i++)
				s = s + "or slot = '" + _searchSlots[i] + "' "
			s = s + ") ";
			// 添加条件：职业
			var classColumn:String = "fit_" + _searchPropClass.toLowerCase().replace(" ", "");
			s = s + "and " + classColumn + " = true ";
			// 添加条件：职能
			s = s + "and (" + "false "
			if(CharacterConst.isTank(_searchPropClass, _searchPropTalent))
				s = s + "or fit_tank = true ";
			if(CharacterConst.isCaster(_searchPropClass, _searchPropTalent))
				s = s + "or fit_caster = true ";
			if(CharacterConst.isMelee(_searchPropClass, _searchPropTalent))
				s = s + "or fit_melee = true ";
			s = s + ") ";
			
			return s;
		}
	}
}